const { app, BrowserWindow, Menu, ipcMain, dialog, shell, globalShortcut } = require('electron')
const path = require('path')
const url = require('url')
const fs = require('fs')
// const Menu = electron.Menu

let win, wintwo

var template = [
    {
        label: '文件',
        submenu: [
            {
                label: '保存到服务器',
                click: () => saveToSvr()
            },
            {
                label: '打开本地文件',
                click: () => openFileDlg()
            },
            {
                label: '保存到本地',
                click: function () { win.webContents.send('to-save-file') }
            }
            ,
            {
                type: 'separator'
            },
            {
                label: '退出',
                click: function () { winOpen('http://www.baidu.com') }
            }
        ]
    },
    {
        label: '帮助',
        submenu: [
            {
                label: '我的github',
                click: () => winOpen('https://github.com/chengang4505')
            },
            {
                label: '我的博客',
                click: () => winOpen('http://blog.techcave.cn')
            },
            {
                label: 'Open DevTools',
                click: () => { win.webContents.openDevTools() }
            },
            {
                label: '关于',
            }
        ]
    }
];
function saveToSvr() {
    win.webContents.send('save-to-server')
}
function winOpen(url) {
    shell.openExternal(url)
}

function openFileDlg() {
    dialog.showOpenDialog(win, {
        properties: ['openFile']
    }, function (files) {
        if (files) {
            fs.readFile(files[0], 'utf-8', function (err, data) {
                event.sender.send('selected-file', data)
            })
        }
    })
}

function saveChanges(filepath, content) {
    fs.writeFile(filepath, content, function (err) {
        if (err) {
            console.log(err);
            return;
        }
    });
}

function createWindow() {
    win = new BrowserWindow({ width: 800, height: 600 })
    wintwo = new BrowserWindow({ width: 800, height: 600, show: false });
    const menu = Menu.buildFromTemplate(template)
    Menu.setApplicationMenu(menu)

    // load the dist folder from Angular
    win.loadURL(url.format({
        pathname: path.join(__dirname, 'dist/index.html'),
        protocol: 'file:',
        slashes: true
    }))

    // Open the DevTools optionally:
    // win.webContents.openDevTools()

    win.on('closed', () => {
        win = null
    })
    isReady = true

    ipcMain.on('open-file-dlg', function (event) {
        // dialog.showOpenDialog()
        openFileDlg();
    })

    ipcMain.on('save-file', (event, arg) => {
        dialog.showSaveDialog(function (fileName) {
            if (fileName === undefined) {
                console.log("You didn't save the file");
                return;
            }
            saveChanges(fileName, arg);
        });
    })

    globalShortcut.register('CommandOrControl+S', () => {
        win.webContents.send('to-save-file')
    })

}

app.on('ready', createWindow)



app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
})

app.on('activate', () => {
    if (win === null) {
        createWindow()
    }
})